package com.mymall.serve.serviceimpl;

import com.google.common.collect.Lists;
import com.mymall.serve.entity.MallProduct;
import com.mymall.serve.pojo.vo.ProductVO;
import com.mymall.serve.service.MallProductService;
import com.mymall.serve.utils.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 产品serviceImpl类
 *
 * @author weiwei
 * @since 2020-04-12 11:48
 **/
@Slf4j
@Service
public class ProductServiceImpl {

    @Autowired
    private MallProductService mallProductService;

    @Autowired
    private CacheUtil cacheUtil;

    /**
     * (测试-缓存)通过id获取商品信息
     *
     * @author weiwei
     * @param productId
     * @date 2020/4/12 13:18
     * @return com.mymall.serve.pojo.vo.ProductVO
     */
    @Cacheable(unless = "#result==null",cacheNames = "fiveMinuteCache",key = "#root.methodName.concat(':').concat(#productId)")
    public ProductVO getProductByIdTest(Long productId) {
        MallProduct mallProduct = mallProductService.getById(productId);
        ProductVO vo = new ProductVO();
        BeanUtils.copyProperties(mallProduct,vo);
        return vo;
    }

    public List<ProductVO> getProductList() {
        List<MallProduct> list = mallProductService.list();
        List<ProductVO> voList = Lists.newArrayList();
        list.stream().forEach( e -> {
            ProductVO vo = new ProductVO();
            BeanUtils.copyProperties(e,vo);
            voList.add(vo);
        });
        return voList;
    }

    /**
     * 获取商品点击量排行榜 0~10
     * @author weiwei
     * @param
     * @param rankKeyName 排行榜名称
     * @date 2020/4/16 15:41
     * @return com.mymall.serve.common.RootResult<java.util.List<com.mymall.serve.pojo.vo.ProductVO>>
     */
    public List<ProductVO> getProductHitList(String rankKeyName) {

        Set<ZSetOperations.TypedTuple<String>> typedTuples = cacheUtil.reverseRangeWithScores(rankKeyName, 0, 9);
        List<ProductVO> productVOS = Lists.newArrayList();
        List<Long> productIds = Lists.newArrayList();
        // 拿出top10列表id
        typedTuples.stream().forEach( e -> {
            productIds.add(Long.parseLong(e.getValue()));
        });
        // 查询出所有id列表转map
        List<MallProduct> products = mallProductService.getListByIds(productIds);
        Map<Long, MallProduct> productMap = products.stream().collect(Collectors.toMap(MallProduct::getId, Function.identity()));

        // 按顺序封装VO列表
        typedTuples.stream().forEach( e -> {
            MallProduct mallProduct = productMap.get(Long.parseLong(e.getValue()));
            ProductVO vo = new ProductVO();
            BeanUtils.copyProperties(mallProduct,vo);
            productVOS.add(vo);
        });
        return productVOS;
    }
}
